<?php
// vim:set expandtab tabstop=3 shiftwidth=3 softtabstop=3 foldclose=all foldmethod=marker:
/**
 * 项目：   5anet
 * 文件：   class/calendar/ShowMenu.class.php
 *
 * 显示菜单
 *
 * PHP Version 5
 *
 * @package:   class.calendar
 * @author:    Mike.G Chinese Name: 黄叶 <hylinux@gmail.com>
 * @license:   http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1 
 * @copyright: www.5anet.com
 * @version:   $Id: $
 * @date:      $Date: $
 */

include_once CLASS_PATH.'main/BaseAction.class.php';

include_once CLASS_PATH.'calendar/CalendarUtil.class.php';


class ShowMenu extends BaseAction {
   /**
    * 数据库的连接
    */
   private $db;

   /**
    * 数据库的连接页面
    */
   private $pre_page = 15;


   /**
    * 构造函数
    * @param:  NULL
    * @return: NULL
    * @access: public
    */
   public function __construct() {
      $this->db = $this->getDB();
   }

   /**
    * 列出当日的消息
    * @param:  NULL
    * @return: NULL
    * @access: public
    */
   public function run() {
      
      $smarty = $this->getSmarty();

      

      $now = getdate();

      $year = $this->getParameter('year');
      if ( !$year ) {
         $year = $now['year'];
      }

      $month = $this->getParameter('mon');
      if ( !$month ) {
         $month = $now['mon'];
      }

      $day = $this->getParameter('day');
      if ( !$day ) {
         $day = $now['mday'];
      }

      if ( !checkdate($month, $day, $year) ) {
         $year = $now['year'];
         $month = $now['mon'];
         $day = $now['mday'];
      }


      $smarty = $this->getSmarty();

      $page = $this->getParameter('page');

      if ( !$page || $page <= 0 ) {
         $page = 1;
      }

      //求总的页面
      $total_page = CalendarUtil::getTotalPage($this->db, $year, $month, $day, $this->pre_page); 

      if ( $page > $total_page && $total_page > 0 ) {
         $page = $total_page;
      }

      //页面
      $smarty->assign('now_page', $total_page==0?0:$page);
      $smarty->assign('total_page', $total_page);

      $begin_page = 1;
      $end_page = $total_page;

      if ( $page <= 10 && $total_page >=10  ) {
         $end_page = 10; 
      } else if ( $page > 10  ) {
         if ( $page % 10 == 0 ) {
            //向前翻
            $end_page = $page; 
            $begin_page = $end_page - 9;
         } else if ( $page % 10 == 1 ) {
            //向后翻
            //确定开始的页数
            $begin_page = $page; 
            if ( $begin_page > $total_page ) {
               $begin_page = $page - 9;
            }
            if ( ( $begin_page + 9 ) > $total_page ) {
               $end_page = $total_page;
            } else {
               $end_page = $begin_page + 9;
            } 
   
         } else {
            $num = $page % 10;
            $pre_num = floor($page / 10 );
            $begin_page = $pre_num * 10 + 1;
            $end_page = $begin_page + 9;
         }
      }

      if ( $end_page > $total_page ) {
         $end_page = $total_page;
      }

      $nav_page_array = array();
      for( $i = $begin_page; $i<=$end_page; $i++ ) {
         array_push($nav_page_array, $i);
      }

      //帖子导航栏
      $smarty->assign('nav_page', $nav_page_array);

      $smarty->assign('nowyear', $year);
      $smarty->assign('nowmonth', $month);
      $smarty->assign('nowday', $day);


      
      $smarty->display('showcalendarmenu.tmpl');


   }




}






?>
